<?php
/**
 * 验证码类
 */
    header('Content-type:image/png');
	class validates{
		private $img;//画布
		private $color;//画布的颜色
		private $seed;  //验证码的内容
		private $num;//验证码的数量
		private $width;//画布的宽度
		private $height;//画布的高度
		private $r;//颜色r
		private $g;//颜色g
		private $b;//颜色b
		private $pointnum;//点数
		private $linenum;
		private $size;  //字体大小
		
		//构造函数
		public function __construct($width=null,$height=null,$num=null,$r=null,$g=null,$b=null,$pointnum=null,$linenum=null,$size=null){
			$this->width=is_null($width)?'150':$width;//宽度
			$this->height=is_null($height)?'50':$height;//高度
			$this->num=is_null($num)?'4':$num;//验证码的数量
			$this->r=is_null($r)?'255':$r;//颜色r
			$this->g=is_null($g)?'255':$g;//颜色g
			$this->b=is_null($b)?'255':$b;//颜色b
			$this->pointnum=is_null($pointnum)?'900':$pointnum;//点的数量
			$this->linenum=is_null($linenum)?'10':$linenum;//线的数量
			$this->size=is_null($size)?'35':$size;//字体大小
			////创建画布
			$this->img=imagecreatetruecolor($this->width,$this->height);
		     //取色
		    $this->color=imagecolorallocate($this->img,$this->r,$this->g,$this->b);
			 //填充
			imagefill($this->img,0,0,$this->color);
		}
		
		//总方法
		public function all(){
			$this->validates();
            $this->point();
            $this->line();
//            输出图像
            imagepng($this->img);
//            释放资源
            imagedestroy($this->img);
		}
		
		//验证码
		public function validates(){
			$str = '';
			$this->seed='zxcvbnmasdfghjklqwertyuiop1234567890ASDFGHJKLPOIUYTREWQZXCVNBM';
			for ($i=0;$i<$this->num;$i++) {
				//(画布宽度/验证码数量)*$i+10;
				$x = ($this->width / $this->num) * $i + 10;
				$y = ($this->height + $this->size) / 2;
				$color = imagecolorallocate($this->img, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
				$text = $this->seed[mt_rand(0, strlen($this->seed) - 1)];
				$str.=$text;
				imagettftext($this->img, $this->size, mt_rand(-45, 45), $x, $y, $color, './Public/simsun.ttc', $text);
			}
			$_SESSION['validate']=$str;
		}
		
		//点点
		public function point(){
			for ($i=0;$i<$this->pointnum;$i++){
				$color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
				imagesetpixel($this->img,mt_rand(0,150),mt_rand(0,50),$color);
			}
		}
		
		//线条
		public function line(){
			for ($i=0;$i<$this->linenum;$i++){
				$color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
				imageline($this->img,mt_rand(0,150),mt_rand(0,50),mt_rand(0,150),mt_rand(0,50),$color);
			}
		}
		
	}
//$pic=new validates();
//$pic->all();
?>